package com.wxq.net;

import android.content.Context;
import android.os.Handler;

public class NetWorkThread {

	Context mContext;
	Thread mThread;
	NetWorkUserInterface mUser;
	Handler mHandler;
	Runnable beforeNetworkRunnable;
	Runnable afterNetworkRunnable;
	
	public NetWorkThread(Context mContext,NetWorkUserInterface user,Handler handler) {
		super();
		this.mContext = mContext;
		this.mUser = user;
		if(handler == null)
		{
			mHandler = new Handler();
		}
		else {
			mHandler = handler;
		}
		
		beforeNetworkRunnable = new Runnable() {
			
			@Override
			public void run() {
				// 开始获得网络数据之前的操作
				mUser.beforeNetwork();
			}
		};
		
		afterNetworkRunnable = new Runnable() {
			
			@Override
			public void run() {
				//得到网络数据之后，设置列表
				mUser.afterNetwork();
			}
		};
		
		mThread = new Thread()
		{
			@Override
			public void run() {
				// 获取网络数据
				mHandler.post(beforeNetworkRunnable);
				
				mUser.getNetworkData();
					
				mHandler.post(afterNetworkRunnable);
			}
		};
	}
	
	public void start() {
		if(mThread!=null)
		{
			mThread.start();
		}
	}
}
